<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</body>
<script>
    var divs = document.getElementsByTagName("div")
    console.log(divs);
    // for (var i = 0; i < 6; i++) {
    //     let item = divs[i]
    //     item.onclick = function () {
    //         console.log(i) // 点击哪个都是 6
    //     }
    // }

    for (var i = 0; i < divs.length; i++) {
        (function (i) {
            let item = divs[i]
            item.onclick = function () {
                console.log(i)
            }
        })(i)
    }
</script>
</html>
